package org.example.myleet.p794;

public class Solution {
    public boolean validTicTacToe(String[] board) {
        int[] scores = new int[128];
        scores['X'] = -1;
        scores['O'] = 1;
        int crossCount = 0, circleCount = 0;
        for (int i = 0; i < 3; ++i) {
            for (int j = 0; j < 3; ++j) {
                char c = board[i].charAt(j);
                if ('X' == c) {
                    ++crossCount;
                }
                if ('O' == c) {
                    ++circleCount;
                }
            }
        }
        if (circleCount == 0 && crossCount == 0) {
            return true;
        }
        if (circleCount > crossCount || crossCount - circleCount > 1) {
            return false;
        }
        boolean win = true;
        boolean crossWin = true;
        for (int i = 0; i < 3; ++i) {
            int score = 0;
            for (int j = 0; j < 3; ++j) {
                char c = board[i].charAt(j);
                score += scores[c];
            }
            if (score == 3) {
                crossWin = false;
                break;
            } else if (score == -3) {
                crossWin = true;
                break;
            } else {
                win = false;
            }
        }
        if (!win) {
            win = true;
            crossWin = true;
            for (int j = 0; j < 3; ++j) {
                int score = 0;
                for (int i = 0; i < 3; ++i) {
                    char c = board[i].charAt(j);
                    score += scores[c];
                }
                if (score == 3) {
                    crossWin = false;
                    break;
                } else if (score == -3) {
                    crossWin = true;
                    break;
                } else {
                    win = false;
                }
            }
        }
        if (!win) {
            win = true;
            crossWin = true;
            int score = scores[board[0].charAt(0)];
            score += scores[board[1].charAt(1)];
            score += scores[board[2].charAt(2)];
            if (score == 3) {
                crossWin = false;
            } else if (score == -3) {
                crossWin = true;
            } else {
                win = false;
            }
        }
        if (!win) {
            win = true;
            crossWin = true;
            int score = scores[board[0].charAt(2)];
            score += scores[board[1].charAt(1)];
            score += scores[board[2].charAt(0)];
            if (score == 3) {
                crossWin = false;
            } else if (score == -3) {
                crossWin = true;
            } else {
                win = false;
            }
        }
        if (win) {
            if (crossWin) {
                if (crossCount - circleCount == 1) {
                    return true;
                }
                return false;
            }
            if (crossCount - circleCount == 0) {
                return true;
            }
            return false;
        }
        return true;
    }
}
